package com.eclink.asset.asset.controller;

import cn.hutool.core.bean.BeanUtil;
import com.eclink.asset.asset.domain.dao.AssetFile;
import com.eclink.asset.asset.domain.vo.AssetFileVO;
import com.eclink.asset.asset.service.AssetFileService;

import com.eclink.asset.pojo.query.AssetItemQuery;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author chenshixian
 */
@Api("资产档案接口")
@RestController
@RequestMapping("/asset/file")
public class AssetFileController extends BaseController {

    @Resource
    private AssetFileService service;

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public ResponseEntity<Page<AssetFile>> list(@RequestBody AssetItemQuery query, Pageable page) {
        List<AssetFile> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public ResponseEntity<String> export(AssetItemQuery query) {
        List<AssetFile> list = service.selectList(query, null);
        ExcelUtil<AssetFileVO> util = new ExcelUtil<>(AssetFileVO.class);
        List<AssetFileVO> voList = BeanUtil.copyToList(list, AssetFileVO.class);
        return ResponseEntity.ok(util.writeExcel(voList, "导出"));
    }

    @ApiOperation("获取详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<AssetFile> getInfo(@PathVariable("id") Long id) {
        AssetFile res = service.selectById(id);

        return ResponseEntity.ok(res);
    }

    @ApiOperation("新增")
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody AssetFile assetItem) {
        return ResponseEntity.ok(service.insert(assetItem));
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody AssetFile assetItem) {
        return ResponseEntity.ok(service.update(assetItem));
    }


    @ApiOperation("删除")
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }


}
